package com.young.common.core.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 12:33 PM
 */
public final class Utils {
    public static final String DEFAULT_CHARSET = "utf8";
    private static final Pattern NBP = Pattern.compile("\\S");

    public Utils() {
    }

    public static boolean isHttpSuccess(int code) {
        return code >= 200 && code < 300;
    }

    public static String url(String uri, Map<String, String> parameters) throws IOException {
        return url(uri, parameters, "utf8");
    }

    public static String url(String uri, Map<String, String> parameters, String charset) throws IOException {
        if (parameters != null && !parameters.isEmpty()) {
            StringBuilder sb = new StringBuilder(uri);
            if (!uri.contains("?")) {
                sb.append("?1=1");
            }

            Iterator var4 = parameters.entrySet().iterator();

            while(var4.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)var4.next();
                sb.append("&").append((String)entry.getKey()).append("=").append(encode((String)entry.getValue(), charset));
            }

            return sb.toString();
        } else {
            return uri;
        }
    }

    public static String encode(String source) throws IOException {
        return encode(source, "utf8");
    }

    public static String encode(String source, String charset) throws IOException {
        return source != null && !source.isEmpty() ? URLEncoder.encode(source, charset) : "";
    }

    public static boolean isNotNull(Object... args) {
        if (args != null && args.length != 0) {
            Object[] var1 = args;
            int var2 = args.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object o = var1[var3];
                if (o == null) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static boolean hasNull(Object... args) {
        if (args != null && args.length != 0) {
            Object[] var1 = args;
            int var2 = args.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object o = var1[var3];
                if (o == null) {
                    return true;
                }
            }

            return false;
        } else {
            return true;
        }
    }

    public static String null2Empty(String value) {
        return value == null ? "" : value;
    }

    public static <T> T null2Default(T value, T def) {
        return value == null ? def : value;
    }

    public static String plain(String value) {
        return value != null && NBP.matcher(value).matches() ? value.trim() : "";
    }

    public static String plain(String value, int length) {
        String target = plain(value);
        return target.length() >= length ? target.substring(0, length) : target;
    }

    public static <T> boolean isEmpty(T[] array) {
        return array == null || array.length == 0;
    }

    public static <T> List<T> array2List(T[] array) {
        return array2List(array, false);
    }

    public static <T> List<T> array2List(T[] array, boolean ignoreNull) {
        return array2List(array, (item) -> {
            return item != null || !ignoreNull;
        });
    }

    public static <T> List<T> array2List(T[] array, Predicate<T> function) {
        List<T> list = new ArrayList();
        if (isEmpty(array)) {
            return list;
        } else {
            Object[] var3 = array;
            int var4 = array.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                T item = (T) var3[var5];
                if (function.test(item)) {
                    list.add(item);
                }
            }

            return list;
        }
    }

    public static <T> Set<T> array2Set(T[] array) {
        return array2Set(array, false);
    }

    public static <T> Set<T> array2Set(T[] array, boolean ignoreNull) {
        return array2Set(array, (item) -> {
            return item != null || !ignoreNull;
        });
    }

    public static <T> Set<T> array2Set(T[] array, Predicate<T> function) {
        Set<T> set = new HashSet();
        if (isEmpty(array)) {
            return set;
        } else {
            Object[] var3 = array;
            int var4 = array.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                T item = (T) var3[var5];
                if (function.test(item)) {
                    set.add(item);
                }
            }

            return set;
        }
    }

    public static <S, T> List<T> wrap(S[] array, Function<S, T> function) {
        return wrap(array, false, function);
    }

    public static <S, T> List<T> wrap(S[] array, boolean ignoreNull, Function<S, T> function) {
        List<T> result = new ArrayList();
        if (isEmpty(array)) {
            return result;
        } else {
            Object[] var4 = array;
            int var5 = array.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                S item = (S) var4[var6];
                T target = function.apply(item);
                if (target != null || !ignoreNull) {
                    result.add(target);
                }
            }

            return result;
        }
    }

    public static <T> boolean contains(T item, T[] array) {
        if (array != null && array.length != 0) {
            Object[] var2 = array;
            int var3 = array.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                T i = (T) var2[var4];
                if (i == item) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }
}
